package com.app.service;

import com.app.utils.SpringContextUtils;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
 * ceshi
 *
 * @author ch
 * @date 2022/12/11 22:34
 */
@Service
public class TestService {

    private final Map<String, Function<String, String>> consumerMap = new HashMap<>();

    @PostConstruct
    private void dispatcherTask() {
        consumerMap.put("A", this::executeTypeA);
        consumerMap.put("B", this::executeTypeB);
    }

    public String executeMethod(String type) throws Exception {
        Function<String, String> function = consumerMap.get(type);
        if (null != function) {
            return function.apply(type);
        }
        TestService testService = SpringContextUtils.getBean(TestService.class);
        testService.test1();
        testService.test2();
        return "参数无效";
    }

    public void test1() throws Exception {
        Thread.sleep(1000);
    }

    public void test2() throws Exception {
        Thread.sleep(2000);
    }

    private String executeTypeA(String type) {
        return type + "-A";
    }

    private String executeTypeB(String type) {
        return type + "-B";
    }
}
